﻿
using Pumptester.ExperimentForms.Base;
using System;
using System.Windows.Forms;
using Business.CentrifugalPump.Perf;
using PLC.Base;
using Business.Base.Model;

namespace Pumptester.ExperimentForms.CentrifugalPump.Perf
{
    public partial class MainFormEnhance : BaseMainForm
    {

        Boolean showHeadRange =true;
        Boolean showPowerRange =false;
        Boolean showEffRange = false;

        public bool ShowHeadRange { get => showHeadRange; set => showHeadRange = value; }
        public bool ShowPowerRange { get => showPowerRange; set => showPowerRange = value; }
        public bool ShowEffRange { get => showEffRange; set => showEffRange = value; }

        public MainFormEnhance(Experiment experiment) : base(experiment)
        {

            this.ChartDrawer = new CentrifugalPumpPerfChart();
            InitializeComponent();
            this.dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
            this.dataGridView.MergeColumnNames.Add("Column0");
            this.dataGridView.MergeColumnNames.Add("Column1");
            this.dataGridView.MergeColumnNames.Add("Column2");
            this.dataGridView.MergeColumnNames.Add("Column3");
            this.dataGridView.MergeColumnNames.Add("Column4");
            this.dataGridView.MergeColumnNames.Add("Column5");
            this.dataGridView.MergeColumnNames.Add("Column6");
            this.dataGridView.MergeColumnNames.Add("Column7");
            this.dataGridView.MergeColumnNames.Add("Column8");
            this.dataGridView.MergeColumnNames.Add("Column9");
    
            this.dataGridView.AddSpanHeader(0, 10, "测量数据");

            this.dataGridView.MergeColumnNames.Add("Column10");
            this.dataGridView.MergeColumnNames.Add("Column11");
            this.dataGridView.MergeColumnNames.Add("Column12");
            this.dataGridView.MergeColumnNames.Add("Column13");
            this.dataGridView.MergeColumnNames.Add("Column14");
            this.dataGridView.MergeColumnNames.Add("Column15");
            this.dataGridView.AddSpanHeader(10, 6, "计算数据");
            this.Experiment = experiment;
            this.dataReadHandle = dataFetch_After;

            this.baseDataGridView = this.dataGridView;
            this.baseDataGridView.ColumnHeaderMouseClick += BaseDataGridView_ColumnHeaderMouseClick;
            CaculateAndReDrawReport();

        }
        
        public MainFormEnhance()
        {
            
            //initDataGridViewExHead();
            InitializeComponent();
            
        }

        private void dataFetch_After(EnvData data)
        {
           

            CentrifugalPumpPerfExperimentRecord record = (CentrifugalPumpPerfExperimentRecord)this.ExperimentService.CaculateDataFromRecord(this.Experiment,data);
            

            this.lblInlet.Text = Math.Round(data.PresureIn, 2).ToString();
            this.lblOutlet.Text = Math.Round(data.PresureOut, 2).ToString();
            this.lblCurrent.Text = Math.Round(data.Current, 2).ToString();
            this.lblVoltage.Text = Math.Round(data.Voltage, 2).ToString();
            this.lblFlow.Text = Math.Round(data.Flow, 2).ToString();
            this.lblPower.Text = Math.Round(data.ShaftPower / 1000.0, 2).ToString();
            this.lblSpeed.Text = Math.Round(data.Speed, 2).ToString();
            this.lblHead.Text = Math.Round(record.WorkingHead, 2).ToString();
        }



        #region 继承的方法和属性

      
        protected override void DrawChart(BaseChartData chartData)
        {
            CentrifugalPumpPerfChartData chart = (CentrifugalPumpPerfChartData)chartData;
            chart.ShowEffRange = this.showEffRange;
            chart.ShowPowerRange = this.ShowPowerRange;
            chart.ShowHeadRange = this.ShowHeadRange;
            this.ChartDrawer.DrawChart(chartData, this.userChart);          
          
        }
        
        protected override void ShowRecordinDataGridView(ExperimentRecord experimentRecord)
        {
            CentrifugalPumpPerfExperimentRecord record = (CentrifugalPumpPerfExperimentRecord)experimentRecord;

            record.ExperimentCode = this.Experiment.ExperimentCode;
            DataGridViewRow row = (DataGridViewRow)this.dataGridView.RowTemplate.Clone();

            DataGridViewTextBoxCell idCell = new DataGridViewTextBoxCell();
            idCell.Value = 0;
            idCell.Tag = false;


            DataGridViewTextBoxCell workFlowCell = new DataGridViewTextBoxCell();
            workFlowCell.Value = record.WorkingFlow.ToString("f6");
            workFlowCell.Tag = true;


            DataGridViewTextBoxCell presureInCell = new DataGridViewTextBoxCell();
            presureInCell.Value = record.PresureIn.ToString("f6");
            presureInCell.Tag = true;

            DataGridViewTextBoxCell presureOutCell = new DataGridViewTextBoxCell();
            presureOutCell.Value = record.PresureOut.ToString("f6");
            presureOutCell.Tag = true;

            DataGridViewTextBoxCell voltageCell = new DataGridViewTextBoxCell();
            voltageCell.Value = record.Voltage.ToString("f6");
            voltageCell.Tag = true;

            DataGridViewTextBoxCell electricCurrentCell = new DataGridViewTextBoxCell();
            electricCurrentCell.Value = record.ElectricCurrent.ToString("f6");
            electricCurrentCell.Tag = true;

            DataGridViewTextBoxCell workHeadCell = new DataGridViewTextBoxCell();
            workHeadCell.Value = record.WorkingHead.ToString("f6");
            workHeadCell.Tag = true;


            DataGridViewTextBoxCell powerCell = new DataGridViewTextBoxCell();
            powerCell.Value = (record.WorkingPower / 1000.0).ToString("f6");
            powerCell.Tag = true;

            DataGridViewTextBoxCell speedCell = new DataGridViewTextBoxCell();
            speedCell.Value = record.WorkingSpeed.ToString("f6");
            speedCell.Tag = true;

            DataGridViewTextBoxCell temperatureCell = new DataGridViewTextBoxCell();
            temperatureCell.Value = record.Temperature.ToString("f6");
            temperatureCell.Tag = true;


            DataGridViewTextBoxCell stpQCell = new DataGridViewTextBoxCell();
            stpQCell.Value = record.StpFlow.ToString("f6");

            stpQCell.Tag = false;


            DataGridViewTextBoxCell stpHeadCell = new DataGridViewTextBoxCell();
            stpHeadCell.Value = record.StpHead.ToString("f6");
            stpHeadCell.Tag = false;

            DataGridViewTextBoxCell stpShaftPowerCell = new DataGridViewTextBoxCell();
            stpShaftPowerCell.Value = record.StpShaftPower.ToString("f6");
            stpShaftPowerCell.Tag = false;


            DataGridViewTextBoxCell stpPumpEfficiencyCell = new DataGridViewTextBoxCell();
            stpPumpEfficiencyCell.Value = record.StpPumpEfficiency.ToString("f6");
            stpPumpEfficiencyCell.Tag = false;

            DataGridViewTextBoxCell stpGroupEfficiencyCell = new DataGridViewTextBoxCell();
            stpGroupEfficiencyCell.Value = record.StpGroupEfficiency.ToString("f6");
            stpGroupEfficiencyCell.Tag = false;

            DataGridViewTextBoxCell staticHeadCell = new DataGridViewTextBoxCell();
            staticHeadCell.Value = record.WorkingStaticHead.ToString("f6");
            staticHeadCell.Tag = false;


            DataGridViewTextBoxCell dynamicHeadHeadCell = new DataGridViewTextBoxCell();
            dynamicHeadHeadCell.Value = record.WorkingDynamicHead.ToString("f6");
            dynamicHeadHeadCell.Tag = false;

            DataGridViewTextBoxCell geoHeadHeadCell = new DataGridViewTextBoxCell();
            geoHeadHeadCell.Value = record.WorkingGeoHead.ToString("f6");
            geoHeadHeadCell.Tag = false;

            DataGridViewTextBoxCell frictionHeadCell = new DataGridViewTextBoxCell();
            frictionHeadCell.Value = record.WorkingFrictionHead.ToString("f6");
            frictionHeadCell.Tag = false;



            row.Cells.Add(idCell);
            row.Cells.Add(workFlowCell);
            row.Cells.Add(presureInCell);
            row.Cells.Add(presureOutCell);
            row.Cells.Add(voltageCell);
            row.Cells.Add(electricCurrentCell);
            row.Cells.Add(powerCell);
            row.Cells.Add(workHeadCell);
            row.Cells.Add(speedCell);
            row.Cells.Add(temperatureCell);
            row.Cells.Add(stpQCell);
            row.Cells.Add(stpHeadCell);
            row.Cells.Add(stpShaftPowerCell);
            row.Cells.Add(stpPumpEfficiencyCell);
            row.Cells.Add(stpGroupEfficiencyCell);
            row.Cells.Add(staticHeadCell);
            row.Cells.Add(dynamicHeadHeadCell);
            row.Cells.Add(geoHeadHeadCell);
            row.Cells.Add(frictionHeadCell);
            row.Tag = record;


            //this.experiment.RecordList.Add(record);
            this.dataGridView.Rows.Add(row);
            int id = this.ExperimentService.SaveExperimentRecord(record);
            record.Id = id;
            refreshIdCell();
            //计算和重画报表
            CaculateAndReDrawReport();
        }

        protected override DynamicParams GetParams()
        {
            CentrifugalPumpPerfExperiment exp = (CentrifugalPumpPerfExperiment)this.Experiment;
            DynamicParams dynamicParams = new DynamicParams(exp.FlowmeterAddress, exp.TransformationRatio, (ushort)exp.PhassAddress);
            return dynamicParams;
        }

        protected override DialogResult ShowEditDialog()
        {
            CentrifugalPumpEditForm form = new CentrifugalPumpEditForm(this.Experiment);
            return form.ShowDialog();
        }
        #endregion


        private void dataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex <1 && e.ColumnIndex>9)
            {
                return;
            }
            DataGridView dataGridView = (DataGridView)sender;

            bool canSet = (bool)dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Tag;
            if (!canSet)
            {
                return;
            }

            String labelText = dataGridView.Columns[e.ColumnIndex].HeaderText;

            String oldValue = dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
            DataGridViewRow row = dataGridView.Rows[e.RowIndex];
            CentrifugalPumpPerfExperimentRecord record = (CentrifugalPumpPerfExperimentRecord)row.Tag;
            ValueEditForm editForm = new ValueEditForm(labelText, oldValue);

            System.Drawing.Point point = Control.MousePosition;
            editForm.Location = point;
            var result = editForm.ShowDialog();

            if (DialogResult.OK == result)
            {
                /*try
                {
                    dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = editForm.ReturnValue;

                }
                catch
                {
                    return;
                }*/
                try
                {
                    switch (e.ColumnIndex)
                    {
                        case 1: record.WorkingFlow = Convert.ToDouble(editForm.ReturnValue); break;
                        case 2: record.PresureIn = Convert.ToDouble(editForm.ReturnValue); break;
                        case 3: record.PresureOut = Convert.ToDouble(editForm.ReturnValue); break;
                        case 4: record.Voltage = Convert.ToDouble(editForm.ReturnValue); break;
                        case 5: record.ElectricCurrent = Convert.ToDouble(editForm.ReturnValue); break;
                        case 6: record.WorkingPower = Convert.ToDouble(editForm.ReturnValue) * 1000.0; break;
                        case 7: record.WorkingHead = Convert.ToDouble(editForm.ReturnValue); break;
                        case 8: record.WorkingSpeed = Convert.ToDouble(editForm.ReturnValue); break;
                        case 9: record.Temperature = Convert.ToDouble(editForm.ReturnValue); break;
                    }
                    dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = editForm.ReturnValue;
                }
                catch (FormatException ex)
                {
                    MessageBox.Show("录入的数据格式错误！");
                    return;
                }

                //重新计算
                EnvData data = new EnvData();
                data.PresureIn = record.PresureIn;
                data.PresureOut = record.PresureOut;
                data.Flow = record.WorkingFlow;
                data.Temperature = record.Temperature;
                data.Speed = record.WorkingSpeed;
                data.Voltage = record.Voltage;
                data.Current = record.ElectricCurrent;
                data.ShaftPower = record.WorkingPower;

                CentrifugalPumpPerfExperimentRecord newRecord = (CentrifugalPumpPerfExperimentRecord)this.ExperimentService.CaculateDataFromRecord(this.Experiment,data);

                //重新刷新数据
                record.StpFlow = newRecord.StpFlow;
                record.StpHead = newRecord.StpHead;
                record.StpShaftPower = newRecord.StpShaftPower;
                record.StpPumpEfficiency = newRecord.StpPumpEfficiency;
                record.StpGroupEfficiency = newRecord.StpGroupEfficiency;
                record.WorkingStaticHead = newRecord.WorkingStaticHead;
                record.WorkingDynamicHead = newRecord.WorkingDynamicHead;
                record.WorkingGeoHead = newRecord.WorkingGeoHead;
                record.WorkingFrictionHead = newRecord.WorkingFrictionHead;

                this.ExperimentService.UpdateExperimentRecord(record);

                dataGridView.Rows[e.RowIndex].Cells[10].Value = record.StpFlow.ToString("F6");
                dataGridView.Rows[e.RowIndex].Cells[11].Value = record.StpHead.ToString("F6"); ;
                dataGridView.Rows[e.RowIndex].Cells[12].Value = record.StpShaftPower.ToString("F6"); ;
                dataGridView.Rows[e.RowIndex].Cells[13].Value = record.StpPumpEfficiency.ToString("F6"); ;
                dataGridView.Rows[e.RowIndex].Cells[14].Value = record.StpGroupEfficiency.ToString("F6");
                dataGridView.Rows[e.RowIndex].Cells[15].Value = record.WorkingStaticHead.ToString("F6");
                dataGridView.Rows[e.RowIndex].Cells[16].Value = record.WorkingDynamicHead.ToString("F6");
                dataGridView.Rows[e.RowIndex].Cells[17].Value = record.WorkingGeoHead.ToString("F6");
                dataGridView.Rows[e.RowIndex].Cells[18].Value = record.WorkingFrictionHead.ToString("F6");
                CaculateAndReDrawReport();

            }
        }

  
    }
}
